/*	$Id: cond.c 1118 2010-12-12 21:05:32Z phrakt $	*/
/*
 * Copyright (c) 2010 Jean-Francois Brousseau <phrakt@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/types.h>
#include <sys/time.h>

#include <errno.h>
#include <stdlib.h>
#include <pthread.h>

#include "private.h"


static void cnp_cond_seterr (int);


/*
 * cnp_cond_init()
 *
 * Returns 0 on success, or -1 on failure.
 */
int
cnp_cond_init(cnp_cond_t *cond, void *attr)
{
	int ret;

	ret = pthread_cond_init(&cond->cc_cond, NULL);
	if (ret != 0) {
		cnp_cond_seterr(ret);
		ret = -1;
	}

	return (ret);
}

/*
 * cnp_cond_destroy()
 *
 * Returns 0 on success, or -1 on failure.
 */
int
cnp_cond_destroy(cnp_cond_t *cond)
{
	int ret;

	ret = pthread_cond_destroy(&cond->cc_cond);
	if (ret != 0) {
		cnp_cond_seterr(ret);
		ret = -1;
	}

	return (ret);
}

/*
 * cnp_cond_broadcast()
 *
 * Returns 0 on success, or -1 on failure.
 */
int
cnp_cond_broadcast(cnp_cond_t *cond)
{
	int ret;

	ret = pthread_cond_broadcast(&cond->cc_cond);
	if (ret != 0) {
		cnp_cond_seterr(ret);
		ret = -1;
	}

	return (ret);
}

/*
 * cnp_cond_signal()
 *
 * Returns 0 on success, or -1 on failure.
 */
int
cnp_cond_signal(cnp_cond_t *cond)
{
	int ret;

	ret = pthread_cond_signal(&cond->cc_cond);
	if (ret != 0) {
		cnp_cond_seterr(ret);
		ret = -1;
	}

	return (ret);
}

/*
 * cnp_cond_timedwait()
 *
 * Returns 0 on success, or -1 on failure.
 */
int
cnp_cond_timedwait(cnp_cond_t *cond, cnp_mutex_t *mutex,
    const struct timespec *ts)
{
	int ret;

	ret = pthread_cond_timedwait(&cond->cc_cond, &mutex->cm_mutex, ts);
	if (ret != 0) {
		cnp_cond_seterr(ret);
		ret = -1;
	}

	return (ret);
}

/*
 * cnp_cond_wait()
 *
 * Returns 0 on success, or -1 on failure.
 */
int
cnp_cond_wait(cnp_cond_t *cond, cnp_mutex_t *mutex)
{
	int ret;

	ret = pthread_cond_wait(&cond->cc_cond, &mutex->cm_mutex);
	if (ret != 0) {
		cnp_cond_seterr(ret);
		ret = -1;
	}

	return (ret);
}

/*
 * cnp_cond_seterr()
 *
 */
static void
cnp_cond_seterr(int errcode)
{
	if (errcode == EINVAL)
		cnp_error_set(CNP_ERR_INVCOND);
	else if (errcode == ENOMEM)
		cnp_error_set(CNP_ERR_NOMEM);
	else if (errcode == ETIMEDOUT)
		cnp_error_set(CNP_ERR_TIMEOUT);
}
